package com.demo.app.common;

import com.demo.app.constant.StateConstant;
import lombok.Data;

/**
 * <p>
 * 响应模型
 * </p>
 *
 * @author zhangxt
 * @date 2023/11/1
 */
@Data
public class Result {
    private String msg = "操作成功";
    private boolean success;
    private Integer code;
    private Object data;

    public Result() {
        this.success = true;
        this.code = StateConstant.SUCCESS;
    }

    public static Result success() {
        return new Result();
    }

    public static Result success(String msg) {
        Result result = new Result();
        result.setMsg(msg);
        return result;
    }

    public static  Result success(Object data) {
        Result result = new Result();
        result.setData(data);
        return result;
    }

    public static  Result success(String msg, Object data) {
        Result result = new Result();
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    public static  Result fail() {
        Result result = new Result();
        result.setSuccess(false);
        return result;
    }

    public static  Result fail(String msg) {
        Result result = new Result();
        result.setMsg(msg);
        result.setSuccess(false);
        return result;
    }

    public static  Result error(String msg, Integer code) {
        Result result = new Result();
        result.setSuccess(false);
        result.setMsg(msg);
        result.setCode(code);
        return result;
    }

    public static  Result error(String msg) {
        Result result = new Result();
        result.setSuccess(false);
        result.setMsg(msg);
        result.setCode(StateConstant.SYS_ERRO);
        return result;
    }

    public static Result toAjax(int rows)
    {
        return rows > 0 ? Result.success() : Result.fail();
    }

    public static Result toAjax(Boolean success)
    {
        return success ? Result.success() : Result.fail();
    }
}
